<template>
  <div class="shouye-container">
    <!-- 导航 -->
    <van-nav-bar
    class="page-nav-bar" fixed>
    <van-button
    class="search-btn"
    slot="title"
    type="info"
    size="small"
    round
    icon="search"
    to="/search"
    >搜索</van-button>
    </van-nav-bar>

    <!-- 频道列表 -->
  <van-tabs class="channel-tabs" v-model="active" animated swipeable>
    <van-tab
    :title="channel.name"
    v-for="channel in channels"
    :key="channel.id"
    >
      <!-- // 频道的文章列表 -->
      <ArticleList ref="ArticleList" :channel="channel" />
    </van-tab>
    <div slot="nav-right" class="placeholder"></div>
    <div slot="nav-right"
    class="hamburger-btn"
    @click="isshow = true"
    to="/article"
    >
      <i class="toutiao toutiao-gengduo"></i>
    </div>
  </van-tabs>
  <!-- 频道弹出层 -->
  <van-popup
    v-model="isshow"
    closeable
    close-icon-position="top-left"
    position="bottom"
    :style="{ height: '100%' }"
  >
  <ChannelEdit
  :my-channels="channels"
  :active="active"
  @update-active="onUpdateActive"
  />
  </van-popup>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getItem } from '@/uitls/storage'
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
export default {
  components: { ArticleList, ChannelEdit },
  data () {
    return {
      active: 0,
      channels: [], // 频道列表
      isshow: false // 控制频道弹出层的展示
    }
  },
  computed: {
    ...mapState(['user'])
  },
  name: 'shouyeIndex',
  created () {
    // 调用获取频道列表
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      try {
        let channels = []
        if (this.user) {
          // 已登录,请求到频道数据
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录
          const locaChannels = getItem('channels')
          if (locaChannels) {
            // 有本地频道数据,则使用
            channels = locaChannels
          } else {
            // 没有本地数据,则请求默认推荐频道列表
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        // 将数据更新到组件中
        this.channels = channels
      } catch (err) {
        console.log(err)
        this.$toast('获取频道列表失败')
      }
    },
    onUpdateActive (index, isshow = true) {
      this.active = index
      // 关闭频道弹出层
      this.isshow = isshow
    }
  }
}
</script>

<style scoped lang="less">
.shouye-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/.channel-tabs {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.9;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}

</style>
